use iced::{Element, Length, Padding, Color};
use iced::widget::{button, text, Button, Text};
use crate::theme::CdbTheme;
use super::{spacing, radius, typography};

/// Button variants following Shadcn UI design
#[derive(Debug, Clone, Copy)]
pub enum ButtonVariant {
    Primary,
    Secondary,
    Destructive,
    Outline,
    Ghost,
    Link,
}

/// Button sizes
#[derive(Debug, Clone, Copy)]
pub enum ButtonSize {
    Small,
    Medium,
    Large,
    Icon,
}

/// Create a modern button with Shadcn UI styling
pub fn modern_button<'a, Message: Clone + 'a>(
    content: impl Into<Element<'a, Message>>,
    variant: ButtonVariant,
    size: ButtonSize,
    theme: &'a CdbTheme,
) -> Button<'a, Message> {
    let padding = match size {
        ButtonSize::Small => Padding::from([spacing::SM, spacing::MD]),
        ButtonSize::Medium => Padding::from([spacing::MD, spacing::LG]),
        ButtonSize::Large => Padding::from([spacing::LG, spacing::XL]),
        ButtonSize::Icon => Padding::from(spacing::MD),
    };

    button(content)
        .style(move |_theme, status| {
            let colors = &theme.colors;

            match variant {
                ButtonVariant::Primary => primary_style(colors, status),
                ButtonVariant::Secondary => secondary_style(colors, status),
                ButtonVariant::Destructive => destructive_style(colors, status),
                ButtonVariant::Outline => outline_style(colors, status),
                ButtonVariant::Ghost => ghost_style(colors, status),
                ButtonVariant::Link => link_style(colors, status),
            }
        })
        .padding(padding)
}

fn primary_style(colors: &crate::theme::ShadcnColors, status: button::Status) -> button::Style {
    match status {
        button::Status::Active => button::Style {
            background: Some(colors.primary.into()),
            text_color: colors.primary_foreground,
            border: iced::Border {
                color: colors.primary,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow {
                color: super::shadow::SM,
                offset: iced::Vector::new(0.0, 1.0),
                blur_radius: 2.0,
            },
        },
        button::Status::Hovered => button::Style {
            background: Some(iced::Color {
                a: 0.9,
                ..colors.primary
            }.into()),
            text_color: colors.primary_foreground,
            border: iced::Border {
                color: colors.primary,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow {
                color: super::shadow::MD,
                offset: iced::Vector::new(0.0, 2.0),
                blur_radius: 4.0,
            },
        },
        button::Status::Pressed => button::Style {
            background: Some(iced::Color {
                a: 0.8,
                ..colors.primary
            }.into()),
            text_color: colors.primary_foreground,
            border: iced::Border {
                color: colors.primary,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow {
                color: super::shadow::SM,
                offset: iced::Vector::new(0.0, 1.0),
                blur_radius: 1.0,
            },
        },
        button::Status::Disabled => button::Style {
            background: Some(iced::Color {
                a: 0.5,
                ..colors.muted
            }.into()),
            text_color: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            border: iced::Border {
                color: iced::Color {
                    a: 0.5,
                    ..colors.border
                },
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
    }
}

fn secondary_style(colors: &crate::theme::ShadcnColors, status: button::Status) -> button::Style {
    match status {
        button::Status::Active => button::Style {
            background: Some(colors.secondary.into()),
            text_color: colors.secondary_foreground,
            border: iced::Border {
                color: colors.border,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
        button::Status::Hovered => button::Style {
            background: Some(iced::Color {
                a: 0.8,
                ..colors.secondary
            }.into()),
            text_color: colors.secondary_foreground,
            border: iced::Border {
                color: colors.border,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow {
                color: super::shadow::SM,
                offset: iced::Vector::new(0.0, 1.0),
                blur_radius: 2.0,
            },
        },
        button::Status::Pressed => button::Style {
            background: Some(iced::Color {
                a: 0.7,
                ..colors.secondary
            }.into()),
            text_color: colors.secondary_foreground,
            border: iced::Border {
                color: colors.border,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
        button::Status::Disabled => button::Style {
            background: Some(iced::Color {
                a: 0.5,
                ..colors.muted
            }.into()),
            text_color: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            border: iced::Border {
                color: iced::Color {
                    a: 0.5,
                    ..colors.border
                },
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
    }
}

fn destructive_style(colors: &crate::theme::ShadcnColors, status: button::Status) -> button::Style {
    match status {
        button::Status::Active => button::Style {
            background: Some(colors.destructive.into()),
            text_color: colors.destructive_foreground,
            border: iced::Border {
                color: colors.destructive,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow {
                color: super::shadow::SM,
                offset: iced::Vector::new(0.0, 1.0),
                blur_radius: 2.0,
            },
        },
        button::Status::Hovered => button::Style {
            background: Some(iced::Color {
                a: 0.9,
                ..colors.destructive
            }.into()),
            text_color: colors.destructive_foreground,
            border: iced::Border {
                color: colors.destructive,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow {
                color: super::shadow::MD,
                offset: iced::Vector::new(0.0, 2.0),
                blur_radius: 4.0,
            },
        },
        button::Status::Pressed => button::Style {
            background: Some(iced::Color {
                a: 0.8,
                ..colors.destructive
            }.into()),
            text_color: colors.destructive_foreground,
            border: iced::Border {
                color: colors.destructive,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow {
                color: super::shadow::SM,
                offset: iced::Vector::new(0.0, 1.0),
                blur_radius: 1.0,
            },
        },
        button::Status::Disabled => button::Style {
            background: Some(iced::Color {
                a: 0.5,
                ..colors.muted
            }.into()),
            text_color: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            border: iced::Border {
                color: iced::Color {
                    a: 0.5,
                    ..colors.border
                },
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
    }
}

fn outline_style(colors: &crate::theme::ShadcnColors, status: button::Status) -> button::Style {
    match status {
        button::Status::Active => button::Style {
            background: Some(iced::Color::TRANSPARENT.into()),
            text_color: colors.foreground,
            border: iced::Border {
                color: colors.border,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
        button::Status::Hovered => button::Style {
            background: Some(colors.accent.into()),
            text_color: colors.accent_foreground,
            border: iced::Border {
                color: colors.border,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
        button::Status::Pressed => button::Style {
            background: Some(iced::Color {
                a: 0.8,
                ..colors.accent
            }.into()),
            text_color: colors.accent_foreground,
            border: iced::Border {
                color: colors.border,
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
        button::Status::Disabled => button::Style {
            background: Some(iced::Color::TRANSPARENT.into()),
            text_color: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            border: iced::Border {
                color: iced::Color {
                    a: 0.5,
                    ..colors.border
                },
                width: 1.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
    }
}

fn ghost_style(colors: &crate::theme::ShadcnColors, status: button::Status) -> button::Style {
    match status {
        button::Status::Active => button::Style {
            background: Some(iced::Color::TRANSPARENT.into()),
            text_color: colors.foreground,
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
        button::Status::Hovered => button::Style {
            background: Some(colors.accent.into()),
            text_color: colors.accent_foreground,
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
        button::Status::Pressed => button::Style {
            background: Some(iced::Color {
                a: 0.8,
                ..colors.accent
            }.into()),
            text_color: colors.accent_foreground,
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
        button::Status::Disabled => button::Style {
            background: Some(iced::Color::TRANSPARENT.into()),
            text_color: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: radius::MD.into(),
            },
            shadow: iced::Shadow::default(),
        },
    }
}

fn link_style(colors: &crate::theme::ShadcnColors, status: button::Status) -> button::Style {
    match status {
        button::Status::Active => button::Style {
            background: Some(iced::Color::TRANSPARENT.into()),
            text_color: colors.primary,
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: 0.0.into(),
            },
            shadow: iced::Shadow::default(),
        },
        button::Status::Hovered => button::Style {
            background: Some(iced::Color::TRANSPARENT.into()),
            text_color: iced::Color {
                a: 0.8,
                ..colors.primary
            },
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: 0.0.into(),
            },
            shadow: iced::Shadow::default(),
        },
        button::Status::Pressed => button::Style {
            background: Some(iced::Color::TRANSPARENT.into()),
            text_color: iced::Color {
                a: 0.6,
                ..colors.primary
            },
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: 0.0.into(),
            },
            shadow: iced::Shadow::default(),
        },
        button::Status::Disabled => button::Style {
            background: Some(iced::Color::TRANSPARENT.into()),
            text_color: iced::Color {
                a: 0.5,
                ..colors.muted_foreground
            },
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: 0.0.into(),
            },
            shadow: iced::Shadow::default(),
        },
    }
}
